﻿using System;
using UnityEngine;

public static class DataReader
{
    public static T Convert<T>(string dataText)
    {
        if (!string.IsNullOrEmpty(dataText))
        {
            try
            {
                if (typeof(T).IsEnum)
                {
                    return (T) Enum.Parse(typeof(T), dataText);
                }
                return (T) System.Convert.ChangeType(dataText, typeof(T));
            }
            catch (Exception)
            {
                object[] args = new object[] { dataText, typeof(T) };
                Debug.LogErrorFormat("Could not convert datatext '{0}' to '{1}'", args);
            }
        }
        return default(T);
    }
}

